home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr53 / forth030.zip / STRUCT.4TH < prev    next >
Text File  |  1993-05-21  |  5KB  |  184 lines

  1. \ STRUCT.4TH  Arrays Structures Memory management Words  2/08/93 bbm
  2. \ for Forth/2
  3. \ Copyright <c> 1993  BLUE STAR SYSTEMS
  4.  
  5. Echo @  Echo ON
  6. \
  7. \ These words let you create arrays of numbers, structures which group
  8. \   various types of data into one logical entity, and arrays of
  9. \   structures.
  10. Echo !
  11.  
  12.  
  13. \ ARRAYS
  14.  
  15. : CARRAY ( n <name>-- Does: i -- n )   CREATE  HERE  OVER ALLOT
  16.          SWAP 0 FILL  DOES>  + ;
  17.  
  18. : ARRAY  ( n <name>-- Does: i -- n )   CREATE  HERE  OVER CELLS ALLOT
  19.          SWAP 0 FILL  DOES>  SWAP CELLS + ;
  20.  
  21. \ Example
  22. \
  23. \ 100 CARRAY XList
  24. \      5 XList C@ .    \ Print value of fifth  element
  25. \ 130 35 XList C!      \ Store a 130 into 35th element
  26. \
  27. \ Same for ARRAY, except use the 32-bit @'s and !'s
  28.  
  29.  
  30.  
  31. ( Memory Structure Definition Creation )
  32.  
  33. : STRUCT      CREATE  0 ( not used ) , HERE      \ Creates structure def.
  34.                       0 ( size     ) , 0  
  35.               DOES>  CREATE  CELL+ @ ALLOT ;
  36. : FIELD       CREATE OVER ( offset ) ,           \ Create byte-length field
  37.                       DUP ( size   ) , +  
  38.               DOES>  @ + ;
  39. : ENDSTRUCT   SWAP ! ;                           \ Stores structure size
  40.  
  41. : SIZEOF      '  PFA 9 + @ ;  IMMEDIATE          \ Size of STRUCT or FIELD
  42.  
  43. : NEST        POSTPONE SIZEOF  FIELD ;           \ Use to nest structures
  44.  
  45.  
  46. \ Example 1 - Date Structure for holding system date and time
  47. \
  48. \ STRUCT DateTimeStruct
  49. \    1 Field Hour
  50. \    1 Field Minute
  51. \    1 Field Seconds
  52. \    1 Field Hundreths
  53. \    1 Field Day
  54. \    1 Field Month
  55. \    2 Field Year
  56. \    2 Field TimeZone
  57. \ ENDSTRUCT
  58. \
  59. \
  60. \ Usage Example 
  61. \
  62. \  DateTimeStruct BeginDate      \ Allocate memory for structure
  63. \
  64. \     4 BeginDate Month C!
  65. \     1 BeginDate Day   C!
  66. \  1993 BeginDate Year  W!
  67. \
  68. \  BeginDate SYS$GetDateTime SYSCALL 2DROP
  69. \
  70. \  : DATE.  DUP Day C@ 1 .R  ." /"  DUP Month C@ 2 .R ." /"  Year W@ U. ;
  71. \
  72.  
  73.  
  74.  
  75. ( TO Variable Memory Structure Field Creation )
  76.  
  77. : C+!       ( b addr -- )   TUCK C@ + SWAP C! ;
  78. : <CTODOES> ( addr -- ? )   %TO @ 0 = IF  C@  ELSE
  79.                             %TO @ 0 > IF  C!  ELSE  C+!  THEN THEN
  80.                             0 %TO ! ;
  81. : CHAR        CREATE DUP  , 1  DUP , +  DOES>       @ + <CTODOES> ;
  82.  
  83. : W+!       ( w addr -- )   TUCK W@ + SWAP W! ;
  84. : <WTODOES> ( addr -- ? )   %TO @ 0 = IF  W@  ELSE
  85.                             %TO @ 0 > IF  W!  ELSE  W+!  THEN THEN
  86.                             0 %TO ! ;
  87. : SHORT       CREATE DUP  , 2  DUP , +  DOES>       @ + <WTODOES> ;
  88.  
  89. : INT         CREATE DUP  , CELL  DUP , +  DOES>       @ +  <TODOES> ;
  90. : INT[]       CREATE OVER , CELLS DUP , +  DOES> @ SWAP CELLS + + <TODOES> ;
  91.  
  92. : ARRAYOF   ( size struct_name-- )  POSTPONE SizeOf
  93.               CREATE  DUP ,  *  HERE OVER  ALLOT
  94.               SWAP 0 FILL  DOES>  TUCK @ * + CELL+ ;
  95.  
  96.  
  97. \ Example 2 - Data Structures using TO variables
  98. \
  99. \ You can intermix any of:
  100. \
  101. \    FIELD  CHAR  SHORT  INT  and INT[]
  102. \
  103. \ in the same structure definition.  To add to a STRUCT definition use:
  104. \
  105. \ size FIELD <fieldname>  creates a size-byte length field named <fieldname>
  106. \      CHAR  <varname>    creates a byte   TO variable
  107. \      SHORT <varname>    creates a 2-byte TO variable
  108. \      INT   <varname>    creates a 4-byte TO variable
  109. \ size INT[] <varname>    creates an array of 4-byte TO variables
  110. \
  111. \
  112. \ To use the fields, precede each field name by the starting address of the
  113. \ structure.
  114. \
  115. \
  116. \ STRUCT DateTimeStruct
  117. \       CHAR Hour
  118. \       CHAR Minute
  119. \       CHAR Seconds
  120. \       CHAR Hundreths
  121. \       CHAR Day
  122. \       CHAR Month
  123. \      SHORT Year
  124. \      SHORT TimeZone
  125. \ ENDSTRUCT
  126. \
  127. \ DateTimeStruct BeginDate      \ Allocate memory for structure
  128. \
  129. \    4 TO BeginDate Month       \ Store numbers into fields
  130. \    1 TO BeginDate Day
  131. \ 1993 TO BeginDate Year
  132. \
  133. \ BeginDate SYS$GetDateTime SYSCALL 2DROP   \ Pass structure to OS/2 call
  134. \
  135. \ : DATE. ( DateTimeStruct -- )   \ Fetch field contents
  136. \         DUP Month 1 .R  ." /"
  137. \         DUP Day   2 .R  ." /"
  138. \             Year     U. ;
  139.  
  140.  
  141. \ Example 3 - Mixed and Nested Structure
  142. \
  143. \ STRUCT Transaction
  144. \      INT TransID
  145. \     NEST DateTimeStruct TransDate     \ Nests DateTime structure
  146. \     CHAR TransType
  147. \ 14 INT[] Hours[]
  148. \ 80 FIELD TransDesc
  149. \ ENDSTRUCT
  150. \
  151. \
  152. \ Transaction Trans1               \ Create transaction structure
  153. \
  154. \ 1000  TO  Trans1 TransID         \ Store 100 into TransID
  155. \
  156. \ Trans1 TransType EMIT            \ Emits the Transaction Type
  157. \
  158. \ 1993  TO  Trans1 TransDate Year  \ Set Transaction date to 1993
  159. \
  160. \   81  TO  Trans1 5 Hours[]       \ Store 81 into  Hours[5]  of Trans1
  161. \
  162. \ Trans1 5 Hours[] .               \ Display  Hours[5]  of Trans1
  163. \
  164. \ " Overtime OK'd by M.E.D."  Trans1 TransDesc "MOVE  \ Store to TransDesc
  165. \
  166. \ Trans1 TransDesc ".              \ Types contents of TransDesc
  167.  
  168.  
  169. \ Example 4 - Creating an array of structures
  170. \
  171. \
  172. \ 50 ArrayOf Transaction Trans[]      \ Create array of 50 transactions
  173. \
  174. \ 50 0 DO
  175. \    I 100 +  TO  I Trans[] TransID        \ Store unique ID's into TransID's
  176. \    1993     TO  I Trans[] TransDate Year \ Store 1993 into year
  177. \    40       TO  I Trans[] 0 Hours[]      \ Store 40 into Hours[0]
  178. \ LOOP
  179. \
  180. \ 23 Trans[] TransDate Year .      \ Show transaction 23's year
  181. \
  182.  
  183.  
  184.